package com.baizhiedu.rsm.util;


import com.baizhiedu.rsm.dao.ClassMessageDao;
import com.baizhiedu.rsm.dao.ScheduleDao;
import com.baizhiedu.rsm.entity.Schedule;
import com.baizhiedu.rsm.entity.Student;
import com.baizhiedu.rsm.entity.StudentMessage;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;


public class ExportExcelForStudent {
	/**
	 * 备份用户的excel数据文件方法。
	 *            根据要清理的条件获得的用户数据信息。
	 * @param
	 * 
	 *       
	 */
	public static void createExcelOfUser(List<StudentMessage> students, HSSFSheet sheetForAuth, ScheduleDao scheduleDao, ClassMessageDao classMessageDao) {

		// 第三步，在sheet中添加表头第0行  0  代表第一行
		HSSFRow rowHead = sheetForAuth.createRow(0);
		// 第四步，创建单元格，并设置值表头 
		rowHead.createCell(0).setCellValue("编号");
		rowHead.createCell(1).setCellValue("班级");
		rowHead.createCell(2).setCellValue("姓名");
		rowHead.createCell(3).setCellValue("年龄");
		rowHead.createCell(4).setCellValue("学历");
		rowHead.createCell(5).setCellValue("来源");
		rowHead.createCell(6).setCellValue("入班测试分数");
		rowHead.createCell(7).setCellValue("是否缴费");
		rowHead.createCell(8).setCellValue("学习态度");
		rowHead.createCell(9).setCellValue("技术能力");
		rowHead.createCell(10).setCellValue("稳定与否");
		rowHead.createCell(11).setCellValue("预热进度");
		rowHead.createCell(12).setCellValue("异常状态");
		rowHead.createCell(13).setCellValue("重点关注");
		rowHead.createCell(14).setCellValue("是否需要支持配合");
		rowHead.createCell(15).setCellValue("备注");
		rowHead.createCell(16).setCellValue("咨询老师");
		rowHead.createCell(17).setCellValue("手机");
		rowHead.createCell(18).setCellValue("邮箱");
		rowHead.createCell(19).setCellValue("微信");
		rowHead.createCell(20).setCellValue("QQ");

		StudentMessage student = new StudentMessage();

		// 第五步，写入实体数据 实际应用中这些数据从数据库得到，
		if (students != null) {
			//    8      从集合的最高下标 递减 添加数据
			for (int i = students.size() - 1; i >= 0; i--) {

				student = students.get(i);

				Integer id = student.getId();
				String banid = classMessageDao.selectByPrimaryKey(student.getClassid()).getClassname();
				String name = student.getName();
				String age = student.getAge();
				String education = student.getEducation();
				String source = student.getSource();
				Double testscore = student.getTestscore();
				String payment = student.getPayment();
				String motivation = student.getMotivation();
				String technical = student.getTechnical();
				String stable = student.getStable();

				String schedule = student.getSchedule();
				Schedule schedule1 = scheduleDao.selectByPrimaryKey(Integer.parseInt(schedule));
				schedule = schedule1.getSname();

				String exceptionstate = student.getExceptionstate();
				String focus = student.getFocus();
				String support = student.getSupport();
				String remark = student.getRemark();
				String zxteacher = student.getZxteacher();
				String phone = student.getPhone();
				String email = student.getEmail();
				String wechat = student.getWechat();
				String qq = student.getQq();

				// 创建行
				HSSFRow row = sheetForAuth.createRow(i + 1);

				row.createCell(0).setCellValue(id);
				row.createCell(1).setCellValue(banid);
				row.createCell(2).setCellValue(name);
				row.createCell(3).setCellValue(age);
				row.createCell(4).setCellValue(education);
				row.createCell(5).setCellValue(source);
				row.createCell(6).setCellValue(testscore);
				row.createCell(7).setCellValue(payment);
				row.createCell(8).setCellValue(motivation);
				row.createCell(9).setCellValue(technical);
				row.createCell(10).setCellValue(stable);
				row.createCell(11).setCellValue(schedule);
				row.createCell(12).setCellValue(exceptionstate);
				row.createCell(13).setCellValue(focus);
				row.createCell(14).setCellValue(support);
				row.createCell(15).setCellValue(remark);
				row.createCell(16).setCellValue(zxteacher);
				row.createCell(17).setCellValue(phone);
				row.createCell(18).setCellValue(email);
				row.createCell(19).setCellValue(wechat);
				row.createCell(20).setCellValue(qq);

			}
		}

	}
	/**
	 * 设置下载文件编码/响应格式
	 * 
	 * @param request
	 * @param response
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public final static void setAttachmentFileName(HttpServletRequest request,
			HttpServletResponse response, String fileName)
			throws UnsupportedEncodingException {

		response.setCharacterEncoding("UTF-8");
		// 下载响应格式
		response.setContentType("application/octet-stream;charset=UTF-8");
		// 防止中文乱码，设置文本的编码格式
		fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");
		response.setHeader("Content-Disposition", "attachment; filename="+ fileName);
	}
}
